
var sCommand = "---";
var bCommandSet = false;
var bCommandExecutable = true;
var colHighlight = "#777722";
var colLowlight = "#225522";
var colNolight = "#001100";
var colActive = "#AAAA33";

var command = {
    code : ["IMP", "HYP", "SHI", "PHA", "PHO", "STOP", "CFM", "EXE"],
    console : ["IMPULSE", "HYPER", "SHIELD", "PHASER", "PHOTON", "STOP", "CONFIRM", "EXECUTE"],
    force : [10,1,100,50,1,"","",""],
    maximum : 8,
    updateSlot : function (param) {
        this.code[param.slot] = param.code;
        this.console[param.slot] = param.console;
        this.force[param.slot]= param.force;
        document.getElementById('console_'+param.slot).innerHTML = param.console;
        document.getElementById('force_'+param.slot).innerHTML = param.force;
    },
    initButtons : function () {
        var i;
        var s;
        for (i = 0; i < command.maximum; i++) {
            var combutton = document.getElementById("com_"+i);
            s = 'highlightComButton("' + i + '")';
            combutton.onmouseover = new Function(s);
            s = 'pressComButton("' + i + '")';
            combutton.onclick = new Function(s);
            document.getElementById('console_'+i).innerHTML = this.console[i];
            document.getElementById('force_'+i).innerHTML = this.force[i];
        }
    },
    updateButtons : function (updates) {
        var u;
        for (u in updates) this.updateSlot (updates[u]);
    }
}

var highlightComButton = function (id) {
    var i, bcolor;
    for (i = 0; i < command.maximum; i++) {
        if (id == i) {
            bcolor = colHighlight;
            if ((command.code[i]=="EXE") && !(bCommandExecutable)) {
                bcolor = colNolight;
            }
        } else {
            bcolor = colLowlight;
            if ((command.code[i]=="EXE") && !(bCommandExecutable)) {
                bcolor = colNolight;
            }
        }
        if (command.code[i]!=sCommand) {
            document.getElementById('com_'+i).style.backgroundColor = bcolor;
        }
    }
};

var showCommand = function() {
  var commanddiv = document.getElementById("command");
  commanddiv.innerHTML = 'Command= ' + sCommand + ' Confirmed=' + bCommandSet + ' Angle='+ angle + ' Vector=(' + vector_x + "," + vector_y + ') Force=' + force;
  if (bCommandSet) {
      commanddiv.style.backgroundColor = colHighlight; }
  else {
      commanddiv.style.backgroundColor = colLowlight;
  }
};

var pressComButton = function(id) {
  switch (command.code[id]) {
    case "CFM":
         bCommandSet = true;
         showCommand();
         var sMessage = 'cmnd=' + sCommand + '&go=0&vector=(' + vector_x + "," + vector_y + ')&force=' + force;
         showAlert(sMessage);
         sendMessage('/command', sMessage);
         break;
    case "EXE":
         if (bCommandExecutable) {
             bCommandSet = true;
             showCommand();
             var sMessage = 'cmnd=' + sCommand + '&go=1&vector=(' + vector_x + "," + vector_y + ')&force=' + force;
             showAlert(sMessage);
             sendMessage('/command', sMessage);
             clearCommandExecutable();
         }
         break;
    default:
         sCommand =  command.code[id];
         bCommandSet = false;
         force = command.force[id];
         showCommand();
         document.getElementById('com_'+id).style.backgroundColor = colActive;
         document.getElementById("paramf").elements["force"].value = force;
  }
}


var setCommandExecutable = function () {
    timerdiv = document.getElementById("timer");
    timerdiv.style.width = "200px";
    timerdiv.style.backgroundColor = colLowlight;
    timerdiv.style.borderColor = colHighlight;
    bCommandExecutable = true;
    document.getElementById('com_'+command.code.indexOf("EXE")).style.backgroundColor = colLowlight;
    showCommand();
}

var clearCommandExecutable = function () {
    timerdiv = document.getElementById("timer");
    timerdiv.style.width = "200px";
    timerdiv.style.backgroundColor = colNolight;
    timerdiv.style.borderColor = colLowlight;
    bCommandExecutable = false;
    bCommandSet = false;
    document.getElementById('com_'+command.code.indexOf("EXE")).style.backgroundColor = colNolight;
    showCommand();
}

var timeCommandExecutable = function () {
    $("#timer").css({"background-color": "#552222", "border-color" : "#AA2222"})
    $("#timer").animate( {"width": "0px"},
            {duration : 5000,
             complete : function () {setCommandExecutable()}});
}

